
#include "Paineis/PainelC.h"
#include "PetLab.h"


PainelC::PainelC(int cx, int cy, int x, int y)
    : Controle(cx, cy, x, y)
{
    for(int i=0; i<Largura(); i++)
        for(int j=0; j<Altura(); j++)
        {
            int raio = Largura();
            int x = i - Largura()/2;
            int y = j - Altura()/2;
			double fator = ((double)::sqrt((float)x*x+y*y))/(raio);
            
            int r = (int)(fator * 64/2)+128+32;
            int g = (int)(fator * 64/2)+128+64+32;
            int b = 255;
            Matriz()[i][j] = RGBA(r,g,b,64);
        }
    
    for(int i=0; i<Largura(); i++)
    {
        Matriz()[i][0]          = RGBA(128,255,128+64+32,128+64);
        Matriz()[i][Altura()-1] = RGBA(128,255,128+64+32,128+64);
    }
    for(int j=0; j<Altura(); j++)
    {
        Matriz()[0][j]           = RGBA(128,255,128+64+32,128+64);
        Matriz()[Largura()-1][j] = RGBA(128,255,128+64+32,128+64);
    }

    Matriz()[0][0] &= 0x00FFFFFF;
    Matriz()[0][Altura()-1] &= 0x00FFFFFF;
    Matriz()[Largura()-1][0] &= 0x00FFFFFF;
    Matriz()[Largura()-1][Altura()-1] &= 0x00FFFFFF;
    
    
/*    
    // Adiciona TELA_A

    m_TelaA = new TelaA(5, 27);
    m_TelaA->Visivel(false);
    //m_TelaA->Abrir();
    m_TelaA->CarregarFuncao(NULL);
    this->AdicionarControle(m_TelaA);

    m_BotaoRe = new BotaoRe(0,0,&TelaA::ParteRe,m_TelaA);
    this->AdicionarControle(m_BotaoRe);

    m_BotaoIm = new BotaoIm(0,0,&TelaA::ParteIm,m_TelaA);
    this->AdicionarControle(m_BotaoIm);
*/

    m_OsciC = new OsciC(10,10+20);
    m_OsciC->Visivel(true);
    this->AdicionarControle(m_OsciC);
}


void PainelC::AbrirSom(std::string strNomeDoArquivo)
{
    PainelA&  painelA  = *(g_PetLab->m_PainelA);
    PainelB&  painelB  = *(g_PetLab->m_PainelB);
    painelA.Visivel(false);
    painelB.Visivel(false);

    PainelC&  painelC  = *(g_PetLab->m_PainelC);
    PainelD&  painelD  = *(g_PetLab->m_PainelD);
    OsciC&    osciC    = *(g_PetLab->m_PainelC->Osci());
    OsciD&    osciD    = *(g_PetLab->m_PainelD->Osci());
    int       cxPetLab = g_PetLab->Largura();
    int       cyPetLab = g_PetLab->Altura();


    Som som;
    som.Abrir(strNomeDoArquivo);
    osciC.CarregarSinal(som.Sinal());
    osciD.CarregarSinal(som.Sinal());
    som.Play();

    painelC.Visivel(false);
    painelD.Visivel(false);

    int xPainelC = 10;
    painelC.PosicaoAbsoluta(xPainelC, painelC.PosicaoY());
    
    int xPainelD = 10;
    painelD.PosicaoAbsoluta(xPainelD, painelD.PosicaoY());
    
    int cxPainelC = cxPetLab-20;
    int cyPainelC = 240;
    painelC.Redimensionar(cxPainelC, cyPainelC);

    int cxPainelD = cxPetLab-20;
    int cyPainelD = 240;
    painelD.Redimensionar(cxPainelD, cyPainelD);

    painelC.Visivel(true);
    painelD.Visivel(true);

    /*g_PetLab->m_PainelA->botaoFecharPainel()->PosicaoX(g_PetLab->m_PainelA->Largura()-25);
    g_PetLab->m_PainelA->botaoFecharPainel()->Visivel(true);*/
}


void PainelC::Redimensionar(int cx, int cy)
{
    Largura(cx);
    Altura(cy);
    
    for(int i=0; i<Largura(); i++)
        for(int j=0; j<Altura(); j++)
        {
            int raio = Largura();
            int x = i - Largura()/2;
            int y = j - Altura()/2;
			double fator = ((double)::sqrt((float)x*x+y*y))/(raio);
            
            int r = (int)(fator * 64/2)+128+32;
            int g = (int)(fator * 64/2)+128+64+32;
            int b = 255;
            Matriz()[i][j] = RGBA(r,g,b,64);
        }
    
    for(int i=0; i<Largura(); i++)
    {
        Matriz()[i][0]          = RGBA(128,255,128+64+32,128+64);
        Matriz()[i][Altura()-1] = RGBA(128,255,128+64+32,128+64);
    }
    for(int j=0; j<Altura(); j++)
    {
        Matriz()[0][j]           = RGBA(128,255,128+64+32,128+64);
        Matriz()[Largura()-1][j] = RGBA(128,255,128+64+32,128+64);
    }

    Matriz()[0][0] &= 0x00FFFFFF;
    Matriz()[0][Altura()-1] &= 0x00FFFFFF;
    Matriz()[Largura()-1][0] &= 0x00FFFFFF;
    Matriz()[Largura()-1][Altura()-1] &= 0x00FFFFFF;
}


bool PainelC::AoBaixarEsq(EventoBaixarEsq ev)
{
    return false;
    
    
    for(int i=0; i<Largura(); i++)
        for(int j=0; j<Altura(); j++)
        {
            int raio = Largura();
            int x = i - Largura()/2;
            int y = j - Altura()/2;
			double fator = ((double)::sqrt((float)x*x+y*y))/(raio);
            
            int r = (int)(fator * 64/2)+128+64+32;
            int g = (int)(fator * 64/2)+128+32;
            int b = 32;
            Matriz()[i][j] = RGBA(r,g,b,128);
        }
    
    for(int i=0; i<Largura(); i++)
    {
        Matriz()[i][0]          = RGBA(255,255,0/*128+64+32*/,255);
        Matriz()[i][Altura()-1] = RGBA(255,255,0/*128+64+32*/,255);
    }
    for(int j=0; j<Altura(); j++)
    {
        Matriz()[0][j]           = RGBA(255,255,0/*128+64+32*/,255);
        Matriz()[Largura()-1][j] = RGBA(255,255,0/*128+64+32*/,255);
    }

    Matriz()[0][0] &= 0x00FFFFFF;
    Matriz()[0][Altura()-1] &= 0x00FFFFFF;
    Matriz()[Largura()-1][0] &= 0x00FFFFFF;
    Matriz()[Largura()-1][Altura()-1] &= 0x00FFFFFF;
    
    ev.janela->Redesenhar(this);
}


bool PainelC::AoSubirEsq(EventoSubirEsq ev)
{
    return false;


    for(int i=0; i<Largura(); i++)
        for(int j=0; j<Altura(); j++)
        {
            int raio = Largura();
            int x = i - Largura()/2;
            int y = j - Altura()/2;
			double fator = ((double)::sqrt((float)x*x+y*y))/(raio);
            
            int r = (int)(fator * 64/2)+128+32;
            int g = (int)(fator * 64/2)+128+64+32;
            int b = 255;
            Matriz()[i][j] = RGBA(r,g,b,64);
        }
    
    for(int i=0; i<Largura(); i++)
    {
        Matriz()[i][0]          = RGBA(128,255,128+64+32,128+64);
        Matriz()[i][Altura()-1] = RGBA(128,255,128+64+32,128+64);
    }
    for(int j=0; j<Altura(); j++)
    {
        Matriz()[0][j]           = RGBA(128,255,128+64+32,128+64);
        Matriz()[Largura()-1][j] = RGBA(128,255,128+64+32,128+64);
    }

    Matriz()[0][0] &= 0x00FFFFFF;
    Matriz()[0][Altura()-1] &= 0x00FFFFFF;
    Matriz()[Largura()-1][0] &= 0x00FFFFFF;
    Matriz()[Largura()-1][Altura()-1] &= 0x00FFFFFF;
    
    ev.janela->Redesenhar(this);
}
